package com.greet.service.impl;

import com.greet.context.BaseContext;
import com.greet.dto.ShoppingCartDTO;
import com.greet.entity.Dish;
import com.greet.entity.Setmeal;
import com.greet.entity.ShoppingCart;
import com.greet.mapper.DishMapper;
import com.greet.mapper.SetmealMapper;
import com.greet.mapper.ShoppingCartMapper;
import com.greet.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public synchronized void add(ShoppingCartDTO shoppingCartDTO) {

        //查询购物车里面是否存在菜品或套餐
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.select(
                ShoppingCart.builder()
                        .userId(BaseContext.getCurrentId())
                        .dishId(shoppingCartDTO.getDishId())
                        .dishFlavor(shoppingCartDTO.getDishFlavor())
                        .setmealId(shoppingCartDTO.getSetmealId()).build()
        );


        if (shoppingCartList != null && shoppingCartList.size() > 0) {
            //有菜品或套餐
            //存在修改数量为+1
            Integer num = shoppingCartList.get(0).getNumber() + 1;
            shoppingCartMapper.update(
                    ShoppingCart.builder()
                            .id(shoppingCartList.get(0).getId())
                            .number(num).build()
            );
        } else {
            //不存在，添加一条购物车数据
            String name = null;
            BigDecimal amount = null;
            String image = null;
            //判断是菜品还是套餐
            if (shoppingCartDTO.getDishId() != null) {
                //菜品
                List<Dish> dishes = dishMapper.select(
                        Dish.builder()
                                .id(shoppingCartDTO.getDishId()).build()
                );
                name = dishes.get(0).getName();
                amount = dishes.get(0).getPrice();
                image = dishes.get(0).getImage();
            } else if (shoppingCartDTO.getSetmealId() != null) {
                //套餐
                List<Setmeal> setmeals = setmealMapper.select(
                        Setmeal.builder()
                                .id(shoppingCartDTO.getSetmealId()).build()
                );
                name = setmeals.get(0).getName();
                amount = setmeals.get(0).getPrice();
                image = setmeals.get(0).getImage();
            }
            //构造购物车实体
            ShoppingCart shoppingCart = ShoppingCart.builder()
                    .name(name)
                    .amount(amount)
                    .number(1)
                    .dishFlavor(shoppingCartDTO.getDishFlavor())
                    .dishId(shoppingCartDTO.getDishId())
                    .setmealId(shoppingCartDTO.getSetmealId())
                    .userId(BaseContext.getCurrentId())
                    .image(image)
                    .createTime(LocalDateTime.now())
                    .build();
            //插入数据库
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    @Override
    public List<ShoppingCart> getList(Long userId) {
        List<ShoppingCart> shoppingCarts =
                shoppingCartMapper.select(ShoppingCart.builder().userId(userId).build());
        return shoppingCarts;
    }

    @Override
    public void clean(Long currentId) {
        shoppingCartMapper.deleteByUserId(currentId);
    }

    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        //获取要数量减一的购物车记录
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.select(
                ShoppingCart.builder()
                        .userId(BaseContext.getCurrentId())
                        .dishId(shoppingCartDTO.getDishId())
                        .setmealId(shoppingCartDTO.getSetmealId())
                        .dishFlavor(shoppingCartDTO.getDishFlavor())
                        .build()
        );
        ShoppingCart shoppingCart = shoppingCartList.get(0);

        //查询数量，如果为1删除记录，如果不为1，让num-1
        if (shoppingCart.getNumber() == 1){
            shoppingCartMapper.deleteById(shoppingCart.getId());
        }else {
            shoppingCart.setNumber(shoppingCart.getNumber() - 1);
            shoppingCartMapper.update(shoppingCart);
        }
    }

}
